<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.terren.webdig.setting.entity.LoginUser"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
LoginUser loginUser = (LoginUser)request.getSession().getAttribute("loginUser");
if (loginUser == null) {
	out.println("<script type=\"text/javascript\" >");
	out.println("top.location.href='"+basePath+"page/common/login.jsp?type=1'");
	out.println("</script>");
	return;
} 

String pageUrl = (String)request.getAttribute("pageUrl");
String pageTitle = (String)request.getAttribute("pageTitle"); 
String pageIcon = (String)request.getAttribute("pageIcon");

if(null == pageUrl){

	String firstUrl = loginUser.getFirstFunction().getFunctionUrl();
	if(null!=firstUrl){//获取第一次登录链接
		pageUrl = firstUrl;
		pageTitle = loginUser.getFirstFunction().getFunctionName();
	}else{
		out.println("<script type=\"text/javascript\" >");
		out.println("top.location.href='"+basePath+"/user/userLogin?type=4'");
		out.println("</script>");
		return;
	}
}
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <title>WebDig网站流量分析决策系统</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="shortcut icon" href="page/icon/computer.png">
    <link rel="stylesheet" href="page/js-lib/bootstrap-4.0.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="page/css/simple-line-icons.css">
    <link rel="stylesheet" href="page/js-lib/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="page/css/styles.css"> 
	<script src="page/js-lib/jquery/jquery.min.js"></script>
	<script src="page/js-lib/popper.js/popper.min.js"></script>
	<script src="page/js-lib/bootstrap-4.0.0/js/bootstrap.min.js"></script>
	<script src="page/js/common/carbon.js"></script>
	<script src="page/js/common/tools.js"></script>
	<style type="text/css">
		.con_title{
			color:#C8CCE8;
		}
	</style>
	<script type="text/javascript">
		var baseUrl="<%=basePath%>";
		//全局ajax控制，用于session超时 无权限时 提示
		$.ajaxSetup({
			cache: false, //close AJAX cache
			contentType:"application/x-www-form-urlencoded;charset=utf-8", 
			complete:function(XHR,textStatus){   
		        var resText = XHR.responseText;
		        if(resText=='ajaxSessionTimeOut'){   
		        	sessionTimeOut();
		        }
		        else if(resText=='ajaxNoLimit'){   
		        	noLimit();
		        }        
		    } 
		});
	
		function sessionTimeOut(){
			alert("用户登录会话已过期，请重新登录！");
			setTimeout('window.top.location.href = "page/common/login.jsp"',2000);
		}
		 
		function noLimit(){
			alert('无相应操作权限，请联系系统管理员！');
		}
		
		
		$(function(){
			createMenu();
		});
		
		/************************top.jsp**************************************************/
		function logout(){
			var flag = confirm("确实要退出登录吗？");
			
			if(flag){
				window.location.href = "user/logout";
			}
		}
		function login(){
		    window.location.href = "user/login";
		}
		function goEdit(){
			window.location.href = "user/opfun?k=userEdit";
		}
		
		/**************************************************************************/
		/**
		 * 生成 菜单
		 */
		function createMenu(){
			var functionJsonStr = '${loginUser.functionJson}';
			var functionJson = eval(functionJsonStr);
			var functionActionKey = '${functionActionKey}';
			functionJson.sort(function(a,b){return a.index>b.index?1:0});
			console.info(functionJson);
			if(functionJson != null){
				var menuHtml="<li class='nav-title'>功能</li>";
				var liHtml="";
				var funHtml="";
				for(var i = 0; i<functionJson.length; i++){
					var oneFunction = functionJson[i];
					var actionValue =oneFunction["actionValue"];
					var functionId =oneFunction["functionId"];
					var v_functionName=oneFunction["functionName"];
					var p_icon=oneFunction["icon"];
					var children = functionJson[i].children;
					var totalLength = functionJson.length;
					var childrenLength = 0;
					if(children!=undefined&&children!=null){
						childrenLength = children.length;
					}
					//子功能项
					if(childrenLength>0){
						//父节点
						liHtml = "<li class='nav-item nav-dropdown' id='p_"+functionId+"'><a href='javascript:void(0)'  onclick='openMenu(this)' class='nav-link nav-dropdown-toggle'>"+v_functionName+"<i class='fa fa-caret-left'></i></a>";						
						//子节点ul
						funHtml = "<ul class='nav-dropdown-items'>";
						var innerUlHtml="";//子节点li
						children.sort(function(a,b){return a.index>b.index?1:0});
						for(var z =0;z<children.length;z++){
							var funChl = children[z];
							var actionUrlChild= funChl["actionUrl"];
							var actionValueChild = funChl["actionValue"];
							var c_functionName =funChl["functionName"];
							var k = funChl["k"];
							var spanChoose = "";//功能被选择后显示的图标
							if(functionActionKey==k){//如果被选中
								spanChoose ="icon-target";//如果该功能被选择则加载的图标
								//展开的父节点
								liHtml = "<li class='nav-item nav-dropdown open' id='p_"+functionId+"'><a href='javascript:void(0)' onclick='openMenu(this)' class='nav-link nav-dropdown-toggle'>"+v_functionName+"<i class='fa fa-caret-left'></i></a>";
							}
							innerUlHtml =innerUlHtml+"<li class='nav-item'><a href='javascript:void(0)' id='fun_"+oneFunction.functionId+"' onclick='goFunction(\"" + actionUrlChild + "\")' class='nav-link'>"+c_functionName+"</a></li>";
						}
						funHtml =funHtml+innerUlHtml+"</ul>";
						liHtml = liHtml+funHtml+"</li>";	
					}else{//没有子功能项
						liHtml = "<li class='nav-item nav-dropdown'><a href='javascript:void(0)' class='nav-link nav-dropdown-toggle'>"+v_functionName+"<i class='fa fa-caret-left'></i></a></li>";
					}
					menuHtml= menuHtml+liHtml;
				}
				//将功能追加到菜单里
				$("#menu").append(menuHtml+"<li class='nav-title'>其它</li>");
			}
		}
		
		/** 跳转 url **/
		function goFunction(actionUrl){
			window.location.href = actionUrl;
		}
		
		function openMenu(t){
			if(t !=null){
				if($(t).parent().hasClass("open")){
					$(t).parent().removeClass('open');
				}else{
					//遍历li标签去掉open
					$("li.nav-dropdown").each(function(){
						$(this).removeClass('open');
					});
					//为点击的里标签添加open
					$(t).parent().addClass("open");
				}
			}	
		} 
	</script>
</head>
<body class="sidebar-fixed header-fixed">
<div class="page-wrapper">
    <nav class="navbar page-header">
        <a href="javascript:void(0);" class="btn btn-link sidebar-mobile-toggle d-md-none mr-auto">
            <i class="fa fa-bars"></i>
        </a>
        <a class="navbar-brand" href="http://www.webterren.com/">
            <img src="page/img/logo.jpg" width = "140" height = "40" alt="logo">
        </a>
        <a href="javascript:void(0);" class="btn btn-link sidebar-toggle d-md-down-none">
            <i class="fa fa-bars"></i>
        </a>
        <h2><b>&nbsp;&nbsp;&nbsp;&nbsp;WebDig网站流量分析决策系统</b></h2>
        <!-- <ul class="breadcrumb">面包屑
			<li><b>&nbsp;&nbsp;&nbsp;&nbsp;WebDig网站流量分析决策系统</b></li>
			<li id="secondCrumb"></li>
			<li class="active" id="thirdCrumb"></li>
		</ul> -->
        <ul class="navbar-nav ml-auto">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="javascript:void(0);" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                    <img src="page/img/longmao.jpg" class="avatar avatar-sm"alt="logo">
                    <span class="small ml-1 d-md-down-none"><b>${loginUser.userName}</b></span>
                </a>
                <div class="dropdown-menu dropdown-menu-right">
                    <a href="#" class="dropdown-item" onclick = "goEdit()">
                        <i class="fa fa-wrench"></i> 设置
                    </a>
                    <a href="#" class="dropdown-item" onclick = "logout()">
                        <i class="fa fa-lock"></i> 退出
                    </a>
                </div>
            </li>
        </ul>
    </nav>

    <div class="main-container">
        <div class="sidebar">
            <nav class="sidebar-nav">
                <ul class="nav" id = "menu">
                    <!-- <li class="nav-item nav-dropdown">
                        <a href="#" class="nav-link nav-dropdown-toggle">
                            <i class="icon icon-target"></i>一级菜单 <i class="fa fa-caret-left"></i>
                        </a>
                        <ul class="nav-dropdown-items">
                            <li class="nav-item">
                                <a href="layouts-normal.html" class="nav-link">
                                    <i class="icon icon-target"></i>二级菜单
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="layouts-fixed-sidebar.html" class="nav-link">
                                    <i class="icon icon-target"></i>二级菜单
                                </a>
                            </li>
                        </ul>
                    </li> -->
                </ul>
            </nav>
        </div>
		<!-- 主体内容 -->
        <div class="content" id="content">
        	<h3 class="con_title"><%=pageTitle%></h3>
        	<hr>
           	<jsp:include page="<%=pageUrl%>" flush="true">
				<jsp:param name="pageTitle" value="<%=pageTitle%>"/>
			</jsp:include>
        </div>
    </div>
</div>
</body>
</html>